package system.view;

import system.cashier.impl.CashierImpl;
import system.commodityManagement.impl.CommodityManagementImpl;
import system.register.impl.ClientImpl;
import system.register.impl.ServerImpl;

import java.awt.*;
import java.util.Random;
import java.util.Scanner;

/**
 * Created by 陈文杰 on 2022/4/13 19:38
 */
public class Main{
    public static void main(String[] args) {

        new ServerImpl().start();//启动服务器线程
        new Main().register();//调用系统方法
    }

    //标记收银系统死循环,true则死循环,false则退出收银功能
    private boolean shutDown = true;
    //全局商品管理对象,方便调用
    CommodityManagementImpl commodityManagement = new CommodityManagementImpl();
    //令牌标记
    private static int token = 0;

    //系统登录视图方法
    public void register() {
        String administratorName = null;//记录管理员姓名
        if (token == 0) {
            System.out.println("**************************欢迎**************************");
            sleep(700);
            System.out.println("**********************即将开始营业***********************");
            sleep(700);
            token++;
        }
        System.out.println("\n<请登录超市收银系统>");
        sleep(1000);
        /*
        获取一个客户端对象,调用登录功能
        如果登录成功则继续,否则通知登录失败
         */
        ClientImpl client = new ClientImpl();
        client.register();//登录
        String isTrue = client.receive();
        if (!isTrue.equals("查无此人")) {
            //continue;
            administratorName = client.getAdministratorName();
            if (isTrue.equals("管理员")) {
                System.out.println("欢迎登陆 管理员 [" + administratorName + "]");
                sleep(1000);
                menu("管理员");
            }
            if (isTrue.equals("收银员")) {
                System.out.println("欢迎登陆 收银员 [" + administratorName + "]");
                sleep(1000);
                menu("收银员");
            }
        }
        if (isTrue.equals("查无此人")) {
            //break;
            String isSuccess = isTrue;
            int errorNum = 1;
            while (isSuccess.equals("查无此人") && errorNum < 3) {
                errorNum++;
                System.out.println("用户名或密码错误,请重新输入");
                client.register();
                isSuccess = client.receive();
            }
            if (!isSuccess.equals("查无此人")) {
                administratorName = client.getAdministratorName();
                if (isSuccess.equals("管理员")) {
                    System.out.println("欢迎登陆 管理员[" + administratorName + "]");
                    sleep(1000);
                    menu("管理员");
                }
                if (isSuccess.equals("收银员")) {
                    System.out.println("欢迎登陆 收银员[" + administratorName + "]");
                    sleep(1000);
                    menu("收银员");
                }

            }
            if (isSuccess.equals("查无此人")) {
                System.out.println("多次输入错误,系统退出!");

            }

        }

    }

    //系统菜单视图方法
    public void menu(String type) {
        Scanner scanner = new Scanner(System.in);
        String num = null;
        System.out.println("********************请选择系统功能**********************");
        System.out.println("1.收银功能 开始营业\t2.商品库存管理功能\t3.任意编号 退出系统 停止营业");
        System.out.print("输入编号:");
        num = scanner.next();
        Random random = new Random();//模拟顾客光临
        switch (num) {
            case "1":
                //调用收银功能
                CashierImpl cashier = null;
                double paymentMoney=0;//暂时保存付款金额
                double  commodityMoney=0;//暂时保存购买商品总金额
                while (shutDown) {
                    cashier = new CashierImpl(commodityManagement);
                    System.out.println("\n等待顾客中...\n");
                    try {
                        Thread.sleep(random.nextInt(8000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("欢迎光临");
                    sleep(1000);
                    commodityManagement.showCommodityAll();
                    sleep(800);
                    System.out.println("\n请告诉我你要买啥(编号+空格):");
                    scanner.nextLine();
                    cashier.scan(scanner.nextLine());
                    cashier.alterCommodityNumber();
                    System.out.println("好的,请稍等~");
                    try {
                        Thread.sleep(random.nextInt(3000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    commodityMoney=cashier.settleAccounts();
                    System.out.println("\n一共需要:" + commodityMoney + "^_^");
                    cashier.payment(commodityMoney);
                    //付款
                    System.out.println("\n请拿好您的小票 欢迎下次光临~");
                    cashier.printBill();
                    System.out.print("继续营业吗[y or else]:");
                    if (!scanner.next().equals("y")) {
                        shutDown = false;
                        System.out.println("正在退出子功能...\n");
                        sleep(1200);
                        System.out.println("子功能已退出\n");
                        menu(type);
                    }
                }

                break;
            case "2":
                if (type.equals("收银员")) {
                    System.out.println("你不是管理员");
                    sleep(1000);
                    System.out.print("是否需要重新登录[y or else]:");
                    if (scanner.next().equals("y")) {
                        new ServerImpl().start();//启动服务器线程
                        new Main().register();//调用系统方法
                        break;
                    }
                    menu(type);
                    break;
                }
                boolean isTrue = true;
                while (isTrue) {//调用商品库存管理功能
                    System.out.println("正在跳转...\n");
                    sleep(2000);
                    System.out.println("****************请选择你的业务****************");
                    System.out.println("1.商品入库\t2.商品出库(检索商品名)\t3.商品出库(检索商品编号)" +
                            "\t4.新增商品\t5.查询全部商品\t6.按编号查询商品\t7.任意编号退出子功能");
                    String name = null;
                    int count = 0, id = 0;
                    switch (scanner.next()) {
                        case "1":
                            System.out.print("请告诉我将要入库的商品名:");
                            name = scanner.next();
                            System.out.print("请告诉我将要入库的数额:");
                            count = scanner.nextInt();
                            commodityManagement.commodityStorage(name, count);
                            break;
                        case "2":
                            System.out.print("请告诉我将要出库的商品名:");
                            name = scanner.next();
                            System.out.print("请告诉我将要出库的数额:");
                            count = scanner.nextInt();
                            commodityManagement.commodityRemoval(name, count);
                            break;
                        case "3":
                            System.out.print("请告诉我将要出库的商品编号:");
                            id = scanner.nextInt();
                            System.out.print("请告诉我将要出库的数额:");
                            count = scanner.nextInt();
                            commodityManagement.commodityRemoval(id, count);
                            break;
                        case "4":
                            System.out.print("请告诉我新入库的商品名:");
                            name = scanner.next();
                            System.out.print("请告诉我此商品的数量:");
                            count = scanner.nextInt();
                            System.out.print("请告诉我此商品的价格:");
                            double price = scanner.nextDouble();
                            commodityManagement.newlyCommodity(name, count, price);
                            break;
                        case "5":
                            commodityManagement.inquireCommodityAll();
                            break;
                        case "6":
                            System.out.print("请告诉我将要查询的商品编号:");
                            id = scanner.nextInt();
                            commodityManagement.inquireCommodityAccordingNum(id);
                            break;
                        default:
                            System.out.println("正在退出子功能...\n");
                            sleep(1200);
                            System.out.println("子功能已退出\n");
                            menu(type);
                            isTrue = false;
                            break;
                    }
                }
                break;
            default:
                System.out.println("正在退出系统...");
                sleep(1200);
                System.out.print("\n辛苦啦~ 好好休息 愿安康");
                sleep(1000);
        }
    }

    //停滞方法
    private void sleep(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
